package server

import (
	"context"
	"encoding/json"
	"longmen/server/config/global"
	"longmen/server/internal/rpc/games/fruit_machine"
	"longmen/server/pkg/db/redis/cache"
	pb "longmen/server/pkg/protobuf"
)

func (s *GameServer) FruitAction(ctx context.Context, req *pb.FruitActionReq) (*pb.FruitActionResp, error) {
	reqStr, _ := json.Marshal(req)
	global.Log.Debugf("下注数据:%v", string(reqStr))
	resp := &pb.FruitActionResp{}
	var err error
	if req.GetGameType() == fruit_machine.GameplayBs {
		//resp, err = s.FruitGame.GamePlayBs(req.GetUserId(), 1, 100)
	} else {
		resp, err = s.FruitGame.GameplayNormal(req.GetUserId(), req.GetBetData())
	}
	respStr, _ := json.Marshal(resp)
	global.Log.Debugf("开奖数据:%v", string(respStr))
	return resp, err
}

func (s *GameServer) FruitConfig(ctx context.Context, req *pb.FruitConfigReq) (*pb.FruitConfigResp, error) {
	resp := &pb.FruitConfigResp{}
	for _, v := range s.FruitGame.Fruit {
		resp.FruitConfig = append(resp.FruitConfig, &pb.FruitConfig{
			Idx:  int32(v.Idx),
			Name: v.Name,
			Site: v.Site,
			Icon: v.Icon,
		})
	}
	for _, v := range s.FruitGame.BetAreaConfig {
		resp.BetAreaConfig = append(resp.BetAreaConfig, &pb.BetAreaConfig{
			BetId: int32(v.BetId),
			Icon:  v.Icon,
		})
	}
	resp.Background = fruit_machine.Background
	if user, err := cache.GetUserFullInfo(int(req.UserId)); err == nil {
		resp.Balance = int32(user.Gold)
	}
	return resp, nil
}
